<!-- 角色管理 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input class="iup_width200" v-model="data.name" size="medium" :placeholder="$t('lan.roleName')">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">{{$t('lan.query')}}</el-button>
        <el-button type="info" size="medium" @click="reset">{{$t('lan.reset')}}</el-button>
        <el-button type="primary" size="medium" @click="add('create','')">{{$t('lan.add')}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="getList" v-loading="loading" :element-loading-text="$t('lan.dataLoading')" empty-text border stripe>
      <div slot="empty">
        <div v-if="loading"></div>
        <div v-else>{{$t('lan.available')}}</div>
      </div>
      <el-table-column type="index" :label="$t('lan.num')" align="center" width="140"></el-table-column>
      <el-table-column prop="name" :label="$t('lan.roleName')" align="center"></el-table-column>
      <el-table-column prop="addTime" :label="$t('lan.createTime')" :formatter="common.formatTime" align="center"></el-table-column>
      <el-table-column prop="content" :label="$t('lan.remark')" align="center"></el-table-column>
      <el-table-column :label="$t('lan.operation')" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="add('edit',scope.row)">{{$t('lan.modify')}}</el-button>
          <el-button type="primary" size="small" @click="addPermissions(scope.row)">{{$t('lan.jurisdiction')}}</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">{{$t('lan.del')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>
    <!-- 分页结束 -->

    <!-- 添加/删除用户角色 -->
    <roleListModal v-if="roleData.showModal" :roleData="roleData" @close="getRoleList" />

    <!-- 角色权限配置 -->
    <permissionsModal v-if="rolePermissions.showDialog" :rolePermissions='rolePermissions' @close="getRoleList"></permissionsModal>
  </div>
</template>
<script>
import roleListModal from 'component/permissions/roleListModal'
import permissionsModal from 'component/permissions/permissionsModal'
export default {
  components: {
    roleListModal,
    permissionsModal
  },
  data () {
    return {
      getList: [],
      data: {
        pageSize: 10,
        currentPage: 1,
        name: '',
      },
      total: 0,
      loading: false,
      showModal: false, //是否显示子组件
      showDialog: false,
      getTime: '',
      roleData: {},
      rolePermissions: {},
    }
  },
  methods: {
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page
      this.getRoleList();//获取角色列表
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val
      this.getRoleList();//获取角色列表
    },
    //查询角色
    changeInput () {
      this.data.currentPage = 1
      this.getTime();
    },
    //查询关键字
    search () {
      this.data.currentPage = 1
      this.getRoleList();//获取角色列表
    },
    //重置列表
    reset () {
      Object.assign(this.$data.data, this.$options.data().data)//重置列表
      this.getRoleList();//获取角色列表
    },
    //获取角色列表
    async getRoleList () {
      this.loading = true
      let url = '/role/queryPage'
      let result = await this.request.httpRequest('POST', url, this.data)
      if (result.code == 200) {
        this.total = result.data.totalRows
        this.data.pageSize = result.data.pageSize
        this.data.currentPage = result.data.currentPage
        this.getList = result.data.datas
      } else {
        this.$message({ type: 'error', message: result.msg })
      }
      this.loading = false
    },
    //是否要删除用户角色列表
    del (row) {
      this.$confirm(this.$t('lan.delRole'), this.$t('lan.prompt'), {
        confirmButtonText: this.$t('lan.confirm'),
        cancelButtonText: this.$t('lan.cancel'),
        type: 'warning'
      }).then(async () => {
        this.loading = true
        let url = `/role/delete/${row.id}`
        let result = await this.request.httpRequest('DELETE', url)
        if (result.code == 200) {
          this.$message({ type: 'success', message: result.msg });
          this.getRoleList();
        } else {
          this.loading = false
          this.$message({ type: 'error', message: result.msg })
        }
      })
    },
    //新增、修改用户角色
    add (type, row) {
      this.roleData = {
        title: type == 'create' ? this.$t('lan.addRole') : this.$t('lan.modifyRole'),
        showModal: true,
        roleId: row.id || '',
        type: type
      }
    },
    //权限配置
    addPermissions (row) {
      this.rolePermissions = {
        showDialog: true,
        roleId: row.id || ''
      }
    },
  },
  mounted () {
    this.getRoleList();
    this.getTime = this.helper.debounce(this.getRoleList)//防抖
  },
  created () {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        this.search();
      }
    };
  }
}
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
</style>